home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / Math / double.asm < prev    next >
Assembly Source File  |  1990-04-07  |  8KB  |  278 lines

  1. *------------------------------------------------------------------
  2. * double.asm: PDC library interface to IEEE double floating point
  3. * library supplied by Commodore.
  4. * Loosely based on the Paul Petersen PDC v1.0 library source
  5. * Maintenance Notes:
  6. * 13Aug88 - Created by Jal
  7. * 18Jan89 - .Fd2s and .Fs2d added (LDH)
  8. *------------------------------------------------------------------
  9.  
  10. AG_OpenLib    equ $00030000
  11. AO_MathLib    equ $00008005
  12. bias    SET    -30
  13.  
  14. libref    MACRO
  15. _LVO\1    EQU    bias
  16. bias    SET    bias-6
  17.     ENDM
  18.  
  19. *------------------------------------------------------------------
  20. * Note: In order to generate the correct biases, these *Must* be in the
  21. * same order as the mathieeedoubbas_lib.fd file.
  22. *------------------------------------------------------------------
  23.  
  24.                libref    IEEEDPFix
  25.                libref    IEEEDPFlt
  26.                libref    IEEEDPCmp
  27.                libref    IEEEDPTst
  28.                libref    IEEEDPAbs
  29.                libref    IEEEDPNeg
  30.                libref    IEEEDPAdd
  31.                libref    IEEEDPSub
  32.                libref    IEEEDPMul
  33.                libref    IEEEDPDiv
  34.                libref    IEEEDPFloor    ; needs release 1.2 or beyond
  35.                libref    IEEEDPCeil    ; needs release 1.2 or beyond
  36.  
  37.                XDEF    _MathIeeeDoubBasBase
  38.                XDEF     .FDopen
  39.  
  40.                XREF    _Alert
  41.                XREF    _exit
  42.                XREF    _OpenLibrary
  43.  
  44. *------------------------------------------------------------------
  45.  
  46.     SECTION    DoubleData,DATA
  47.  
  48. _MathIeeeDoubBasBase dc.l 0
  49. MATHName    dc.b    'mathieeedoubbas.library',0
  50.  
  51.     SECTION    Double,CODE
  52.  
  53. *
  54. *    Make sure the MathIeeeDoubBasBase is loaded correctly
  55. *
  56.  
  57. .FDopen:
  58.     movem.l D0-D1/A0-A1,-(A7)    ; save temporary registers
  59.     pea    0            ; any version will do....
  60.     pea     MATHName        ; Load the name of library
  61.     jsr    _OpenLibrary        ; Open the library
  62.     addq    #8,A7
  63.     move.l  D0,_MathIeeeDoubBasBase    ; Save the return code
  64.     bne.s   1$            ; If non-zero then OK
  65.  
  66.     pea    0            ; Parameter for Alert.
  67.     pea    AG_OpenLib!AO_MathLib    ; Signal an error.
  68.     jsr    _Alert
  69.     addq    #8,A7
  70.     pea     100            ;     push return code
  71.     jsr     _exit            ;     and exit
  72. 1$:
  73.     movem.l (A7)+,D0-D1/A0-A1     ; restore temporaries
  74.     rts
  75.  
  76. *-------------------------------------------------------------------
  77. * Do a one parameter MathIeeeDouble function.
  78. *-------------------------------------------------------------------
  79. .FDdo1:
  80.     tst.l    _MathIeeeDoubBasBase
  81.     bne.s    1$
  82.     jsr    .FDopen
  83. 1$:
  84.     movem.l    A6,-(A7)
  85.     move.l    _MathIeeeDoubBasBase,A6    
  86.     add.l    4(A7),A6
  87.     jsr    (A6)
  88.     movem.l    (A7)+,A6
  89.     addq    #4,A7        ; unstack the function offset.
  90.     rts
  91.  
  92. *-------------------------------------------------------------------
  93. * Do a two parameter MathIeeeDouble function.
  94. *-------------------------------------------------------------------
  95. .FDdo2
  96.     tst.l    _MathIeeeDoubBasBase
  97.     bne.s    1$
  98.     jsr    .FDopen
  99. 1$:
  100.     movem.l    D2-D3/A6,-(A7)
  101.     movem.l (A0),D2-D3
  102.     move.l    _MathIeeeDoubBasBase,A6    
  103.     add.l    12(A7),A6
  104.     jsr    (A6)
  105.     movem.l    (A7)+,D2-D3/A6
  106.     addq    #4,A7        ; Unstack the function offset.
  107.     rts
  108.  
  109. *********************************************************************
  110. *
  111. *    Double multiply arguments are in D0,D1 & (A0)
  112. *
  113.     XDEF    .FDmul
  114. .FDmul:
  115.     pea    _LVOIEEEDPMul
  116.     jmp     .FDdo2
  117.  
  118. *********************************************************************
  119. *
  120. *    Double divide arguments are in D0,D1 & (A0)
  121. *
  122.     XDEF    .FDdiv
  123. .FDdiv:
  124.     pea    _LVOIEEEDPDiv
  125.     jmp     .FDdo2
  126.  
  127. *********************************************************************
  128. *
  129. *    Double add arguments are in D0,D1 & (A0)
  130. *
  131.     XDEF    .FDadd
  132. .FDadd:
  133.     pea    _LVOIEEEDPAdd
  134.     jmp     .FDdo2
  135.  
  136. *********************************************************************
  137. *
  138. *    Double subtract arguments are in D0,D1 & (A0)
  139. *
  140.     XDEF    .FDsub
  141. .FDsub:
  142.     pea    _LVOIEEEDPSub
  143.     jmp     .FDdo2
  144.  
  145. *********************************************************************
  146. *
  147. *    Double negate argument is in D0,D1
  148. *
  149.     XDEF    .FDneg
  150. .FDneg:
  151.     pea    _LVOIEEEDPNeg
  152.     jmp     .FDdo1
  153.  
  154. *********************************************************************
  155. *
  156. *    Double compare arguments are in D0,D1 and (A0)
  157. *
  158.     XDEF    .FDcmp
  159. .FDcmp:
  160.     pea    _LVOIEEEDPCmp
  161.     jmp     .FDdo2
  162.  
  163. *********************************************************************
  164. *
  165. *    Convert long to double : Input in D0
  166. *
  167.     XDEF    .Fl2d
  168. .Fl2d:
  169.     pea    _LVOIEEEDPFlt
  170.     jmp     .FDdo1
  171.  
  172. *********************************************************************
  173. *
  174. *    Convert double to long : Input in D0,D1
  175. *
  176.     XDEF    .Fd2l
  177. .Fd2l:
  178.     pea    _LVOIEEEDPFix
  179.     jmp     .FDdo1
  180. *********************************************************************
  181. *
  182. *       Convert double to single : Input in D0:D1 : Output in D0
  183. *
  184.                XDEF    .Fd2s
  185. .Fd2s
  186.                move.l    d2,-(sp)
  187. DoMant                                   ; Chop mantissa down to size (23 bits)
  188.                rol.l    #3,d1          ; Chopping ain't nice, but it's an
  189.                andi.l    #7,d1          ; expedient.
  190.                move.l    d0,d2
  191.                andi.l    #$FFFFF,d2
  192.                lsl.l    #3,d2
  193.                or.l    d2,d1
  194. DoExp
  195.                move.l    d0,d2          ; Shrink exp from 11 to 8 bits
  196.                swap    d2             ; and excess-1023 to excess-127
  197.                lsr.l    #4,d2
  198.                andi.l    #$07ff,d2
  199.                cmpi.w    #1151,d2       ; 1023+128 = 1151 (Max exponent)
  200.                ble.s    ExpNotHi
  201.                move.w    #255,d2        ; Overflow ... result effectively inf.
  202.                bra    ExpOK
  203. ExpNotHi
  204.                cmpi.w    #896,d2        ; 1023-127 = 896 (Min exponent)
  205.                bge.s    ExpNotLo
  206.                moveq.l    #0,d1          ; Underflow ... result effectively 0
  207.                bra    done
  208. ExpNotLo
  209.                sub.w    #896,d2        ; Adjust exponent's bias
  210. ExpOK
  211.                swap    d2             ; Put exponent in its place
  212.                lsl.l    #7,d2
  213. Combine
  214.                or.l    d2,d1          ; Merge exponent and mantissa
  215. done
  216.                andi.l    #$80000000,d0  ; Merge with sign
  217.                or.l    d1,d0
  218.                move.l    (sp)+,d2       ; Shave and a haircut .....
  219.                rts                     ; two bits!
  220.  
  221. *********************************************************************
  222. *
  223. *       Convert single to double : Input in D0 : Output in D0:D1
  224. *
  225.                XDEF    .Fs2d
  226. .Fs2d:
  227.                movem.l    d2-d3,-(sp)
  228.                move.l    d0,d1
  229.                ror.l    #3,d1
  230.                move.l    d1,d2
  231.                and.l    #$FFFFF,d2     ; Nab the upper mantissa in d2
  232.                and.l    #$E0000000,d1  ; Isolate the lower in d1
  233. ExpandExp
  234.                move.l    d0,d3
  235.                swap    d3
  236.                lsr.l    #7,d3
  237.                and.l    #$FF,d3
  238.                add.l    #896,d3        ; Now expand to 11 bits (excess-1023)
  239.                lsl.l    #4,d3
  240.                swap    d3
  241.                and.l    #$80000000,d0  ; Save the sign, toss the rest
  242.                or.l    d3,d0          ; Merge the new exponent w/sign
  243.                or.l    d2,d0          ; Likewise the mantissa
  244.                movem.l    (sp)+,d2-d3
  245.                rts                    ; return to caller
  246.  
  247. *********************************************************************
  248. *
  249. *    Implement the abs function.: Input in D0/D1 : Output in D0:D1
  250. *
  251.     XDEF    _abs
  252. _abs:
  253.     movem.l 4(A7),D0-D1
  254.     pea    _LVOIEEEDPAbs
  255.     jmp    .FDdo1
  256.  
  257. *********************************************************************
  258. *
  259. *    Implement the floor function.: Input in D0/D1 : Output in D0:D1
  260. *
  261.     XDEF    _floor
  262. _floor:
  263.     movem.l 4(A7),D0-D1
  264.     pea    _LVOIEEEDPFloor
  265.     jmp    .FDdo1
  266.  
  267. *********************************************************************
  268. *
  269. *    Implement the ceil function.: Input in D0/D1 : Output in D0:D1
  270. *
  271.     XDEF    _ceil
  272. _ceil:
  273.     movem.l 4(A7),D0-D1
  274.     pea    _LVOIEEEDPCeil
  275.     jmp    .FDdo1
  276.  
  277.     END
  278.